package app.server.project.main.service.db.dao.user;

import app.server.project.main.service.db.entity.user.AccountEntity;
import app.server.project.main.service.db.mapper.user.AccountMapper;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author Administrator
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2025-01-16 15:25:22
 */
@Repository
public class AccountDao extends ServiceImpl<AccountMapper, AccountEntity> {


    @Cacheable(key = "#openId+':'+#channel", cacheNames = "app.server.project.main.service.db.dao.AccountDao")
    public AccountEntity getDataByOpenId(String openId, int channel) {
        var data = this.getOne(Wrappers.lambdaQuery(AccountEntity.class)
                .eq(AccountEntity::getChannel, channel)
                .eq(AccountEntity::getOpenId, openId));
        if (Objects.isNull(data)) {
            //缓存没有,就往库中写入一条数据
            data = AccountEntity.of(openId, IdUtil.getSnowflakeNextId(), channel);
        }
        data.setSynDb(true);
        return data;
    }

    @CacheEvict(key = "#openId+':'+#channel", cacheNames = "app.server.project.main.service.db.dao.AccountDao")
    public void delCache(String openId, int channel) {

    }

    @Cacheable(key = "#userid", cacheNames = "app.server.project.main.service.db.dao.getOpenIdByUserid")
    public AccountEntity getOpenIdByUserid(long userid) {
        var ret = this.getOne(Wrappers.<AccountEntity>lambdaQuery()
                .eq(AccountEntity::getUserId, userid), false);
        if (Objects.isNull(ret)) {
            ret = new AccountEntity();
        }
        return ret;
    }


//    public long getRegNum(LocalDateTime start, LocalDateTime end) {
//        return this.count()
//    }
}




